#!/bin/sh

#set -x

export TEXTDOMAIN=logout_gui

X_SERVER=`type X|awk '{print $3}'`
X=`readlink $X_SERVER`
MSG_X="$(gettext "Active graphical server"): <b>$X</b>"
if pidof jwm &>/dev/null ; then
	MSG_WM="$(gettext "Active window manager"): <b>JWM</b>"
	RESTART_MENUAPP="$(gettext 'Restart window manager')"
	MENUAPP=jwm
elif pidof lxpanel &>/dev/null ; then
	RESTART_MENUAPP="$(gettext 'Restart LXPanel')"
	MENUAPP=lxpanel
	if pidof openbox &>/dev/null ; then
		MSG_WM="$(gettext "Active window manager"): <b>Openbox</b>"
		RESTART_MENUAPP="$(gettext 'Restart LXPanel+Openbox')"
	fi
elif pidof openbox &>/dev/null ; then
	MSG_WM="$(gettext "Active window manager"): <b>Openbox</b>"
	RESTART_MENUAPP="$(gettext 'Restart Openbox')"
	MENUAPP=openbox
fi

S='
<window title="'$(gettext 'Puppy Log Out')'" icon-name="gtk-preferences" window-position="1">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed puppy_config.svg 60 "$MSG_WM" "$MSG_X"`"'
  <vbox spacing="10" space-expand="true" space-fill="true">
  <frame>
    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Reboot')'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon reboot.svg huge`"'
        <action>exit:reboot</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Shut Down')'</label>
         '"`/usr/lib/gtkdialog/xml_button-icon shutdown.svg huge`"'
        <action>exit:poweroff</action>
      </button>
    </hbox>
  
    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">
      <button image-position="2">
        <label>'$(gettext 'Restart graphical server')'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon graphics_restart.svg huge`"'
        <action>exit:restartx</action>
      </button>
      <button image-position="2">
        <label>'$(gettext 'Exit to Prompt')'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon terminal.svg huge`"'
        <action>exit:logout</action>
      </button>
    </hbox>
    
    <hbox spacing="10" homogeneous="true" space-expand="true" space-fill="true">'
      if [ "$MENUAPP" ]; then S=$S'
        <button image-position="2">
          <label>'${RESTART_MENUAPP}'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon wm_restart.svg huge`"'
          <action>exit:restartmenuapp</action>
        </button>'
      else
        S=$S'<text><label>""</label></text>'
      fi
      if [ "`which xlock`" ]; then S=$S'
        <button image-position="2">
          <label>'$(gettext 'Lock screen')'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon screen_lock.svg huge`"'
          <action>/usr/local/apps/Xlock/AppRun</action>
        </button>'
      elif [ "`grep -F 'jwm' <<< $(ps -eo pid,command)`" ]; then S=$S'
        <button image-position="2">
          <label>'$(gettext 'Rebuild Menu')'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon menu.svg huge`"'
          <action>exit:refreshjwm</action>
        </button>'
      else
        S=$S'<text><label>""</label></text>'
      fi
  
    S=$S'</hbox>
  </frame>
  </vbox>
</vbox>
</window>'

export logout="$S"

. /usr/lib/gtkdialog/xml_info gtk > /dev/null #build bg_pixmap for gtk-theme
eval `gtkdialog -p logout`
case $EXIT in
  logout)exec wmexit ;;
  reboot)exec wmreboot ;;
  poweroff)exec wmpoweroff ;;
  restartx)restartwm ;;
  restartmenuapp)
	case $MENUAPP in
		openbox) openbox --restart ;;
		jwm) jwm -restart ;;
		lxpanel)
			lxpanelctl restart
			pidof openbox &>/dev/null && openbox --restart
			;;
	esac
	;;
  refreshjwm)fixmenus; jwm -reload ;;
  *)exit ;;
esac
